﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Ghasam.Business.Services;
using Ghasam.Business.Domain.Entity;
using Mani.Common;
using System.Collections.Generic;

public partial class EmployeeLeavePage : System.Web.UI.Page {

    private IEmployeeService employeeService;
    private IEmployeeLeaveService employeeLeaveService;

    public IEmployeeService EmployeeService { 
        get { return employeeService; }
        set { employeeService = value; }
    }

    public IEmployeeLeaveService EmployeeLeaveService {
        get { return employeeLeaveService; }
        set { employeeLeaveService = value; }
    }

    protected void Page_Load(object sender, EventArgs e) {
        if (!Page.IsPostBack) {
            GetLeaves(0, true);
            GetEmployees();
        }
    }

    private void GetEmployees() {
        IList<Employee> emps = employeeService.SearchEmployee("", "", null, null, true);

        ddlEmployees.DataSource = emps;
        ddlEmployees.DataBind();

        ddlOffEmployee.DataSource = emps;
        ddlOffEmployee.DataBind();
    }

    private void GetLeaves(int pageNumber, bool setPageCount) {
        
        DateTime startDate = DateTime.Now.AddDays(-7);
        DateTime endDate = DateTime.Now;

        if (!String.IsNullOrEmpty(txtStartDate.Text))
            startDate = MyPersianDate.PersianToDateTime(txtStartDate.Text);

        if (!String.IsNullOrEmpty(txtEndDate.Text))
            endDate = MyPersianDate.PersianToDateTime(txtEndDate.Text);

        Employee employee = null;

        if (Convert.ToInt32(ddlEmployees.SelectedValue) != -1)
            employee = EmployeeService.GetById(Convert.ToInt32(ddlEmployees.SelectedValue));

        if (setPageCount)
            gvEmployeeLeaves.VirtualItemCount = EmployeeLeaveService.CountEmployeeLeaves(employee, startDate, endDate);

        gvEmployeeLeaves.DataSource = EmployeeLeaveService.GetEmployeeLeaves(employee, startDate, endDate, 15, pageNumber);
        gvEmployeeLeaves.DataBind();

    }

    protected void btnSearch_Click(object sender, EventArgs e) {
        GetLeaves(0, true);
        lblMessage.Text = "";
    }

    protected void btnSave_Click(object sender, EventArgs e) {

        EmployeeLeave employeeLeave = null;

        if (!String.IsNullOrEmpty(txtOffId.Text))
            employeeLeave = employeeLeaveService.GetEmployeeLeave(Convert.ToInt32(txtOffId.Text));
        else
            employeeLeave = new EmployeeLeave();

        employeeLeave.Employee = employeeService.GetById(Convert.ToInt32(ddlOffEmployee.SelectedValue));
        employeeLeave.FromDate = MyPersianDate.PersianToDateTime(txtOffFromDate.Text);
        employeeLeave.ToDate = MyPersianDate.PersianToDateTime(txtOffEndDate.Text);
        employeeLeave.LeaveWithoutPay = cbOffWithoutPayment.Checked;
        employeeLeave.Hours = Convert.ToSingle(txtOffHoursDuration.Text);

        employeeLeaveService.SaveOrUpdate(employeeLeave);

        lblMessage.Text = "مرخصی با موفقیت ذخیره شد";
        ResetForm();
        GetLeaves(0, true);
    }

    private void ResetForm() {
        ddlOffEmployee.SelectedIndex = -1;
        txtOffEndDate.Text = "";
        txtOffFromDate.Text = "";
        txtOffHoursDuration.Text = "";
        txtOffId.Text = "";
    }

    protected void gvEmployeeLeaves_SelectedIndexChanged(object sender, EventArgs e) {
        int id = Convert.ToInt32(gvEmployeeLeaves.DataKeys[gvEmployeeLeaves.SelectedIndex].Value);

        EmployeeLeave el = employeeLeaveService.GetEmployeeLeave(id);

        ListItem item = ddlOffEmployee.Items.FindByValue(el.Employee.Id.ToString());
        if (item != null) {
            ddlOffEmployee.ClearSelection();
            item.Selected = true;
        }

        txtOffEndDate.Text = MyPersianDate.DateTimeToPersian(el.ToDate);
        txtOffFromDate.Text = MyPersianDate.DateTimeToPersian(el.FromDate);
        txtOffHoursDuration.Text = el.Hours.ToString();
        txtOffId.Text = el.Id.ToString();
        lblMessage.Text = "";
    }

    protected void gvEmployeeLeaves_RowDeleting(object sender, GridViewDeleteEventArgs e) {
        int id = Convert.ToInt32(gvEmployeeLeaves.DataKeys[e.RowIndex].Value);
        employeeLeaveService.Delete(employeeLeaveService.GetEmployeeLeave(id));
        GetLeaves(0, true);
        lblMessage.Text = "";
        ResetForm();
    }

    protected void gvEmployeeLeaves_PageIndexChanging(object sender, GridViewPageEventArgs e) {
        gvEmployeeLeaves.PageIndex = e.NewPageIndex;
        GetLeaves(e.NewPageIndex, false);
        lblMessage.Text = "";
    }
}
